﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using DevExpress.XtraEditors;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmPromotion : Form
    {
        #region "variable"
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        private DataRow _dr;
        private bool _insert = false;
        private bool _edit = false;
        private bool _boqua = false;
        private TextEdit[] list;
        #endregion

        #region "Form methods"
        public frmPromotion()
        {
            InitializeComponent();
        }

        private void frmPromotion_Load(object sender, EventArgs e)
        {
            _boqua = true;
            _insert = false;
            _edit = false;
            CheckButton();
            LoadData();
            list = new TextEdit[] { txtTenChuongTrinh, txtDoiTuong, txtGiaTri };
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            _insert = true;
            _edit = false;
            _boqua = false;
            CheckButton();
            //---------------
            ResetControl();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (Validation(list))
                {                   
                    if (_insert)
                    {
                        string tenchuongtrinh = txtTenChuongTrinh.Text;
                        string noidung = memoEdit_NoiDung.Text;
                        string giatri = txtGiaTri.Text;
                        string doituong = txtDoiTuong.Text;
                        DateTime ngaybatdau = Convert.ToDateTime(dateEdit_NgayBatDau.EditValue);
                        DateTime ngayketthuc = Convert.ToDateTime(dateEdit_NgayKetThuc.EditValue);
                        AddData(tenchuongtrinh, noidung, giatri, doituong, ngaybatdau, ngayketthuc);
                    }
                    else if (_edit)
                    {
                        UpdateData();
                    }
                    LoadData();
                    _boqua = true;
                    _insert = false;
                    _edit = false;
                    CheckButton();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            _edit = true;
            _insert = false;
            _boqua = false;
            CheckButton();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            _boqua = true;
            _insert = false;
            _edit = false;
            CheckButton();
            //---------------
            ResetControl();
        }

        private void gridView_Data_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                _dr = gridView_Data.GetDataRow(e.FocusedRowHandle);
                LoadToControl(_dr);
                btnSua.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region "Coding"
        private void LoadData()
        {
            try
            {
                LoadPromotion();
                gridControl_Data.DataSource = ds.Tables["TablePromotion"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadPromotion()
        {
            try
            {
                if (ds.Tables["TablePromotion"] != null)
                    ds.Tables["TablePromotion"].Clear();
                string sql = @"usp_AllPromotion";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TablePromotion");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddData(string tenchuongtrinh, string noidung, string giatri, string doituong, DateTime ngaybatdau, DateTime ngayketthuc)
        {
            try
            {
                string sql = @"usp_InsertPromotion @tenchuongtrinh = @ten, @noidung = @noidung, @giatri = @giatri,
                                @doituong = @doituong, @ngaybatdau = @ngaybd, @ngayketthuc = @ngaykt";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@ten", tenchuongtrinh);
                cmd.Parameters.AddWithValue("@noidung", noidung);
                cmd.Parameters.AddWithValue("@giatri", giatri);
                cmd.Parameters.AddWithValue("@doituong", doituong);
                cmd.Parameters.AddWithValue("@ngaybd", ngaybatdau);
                cmd.Parameters.AddWithValue("@ngaykt", ngayketthuc);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Insert thành công");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateData()
        {
            try
            {
                _dr["TenChuongTrinh"] = txtTenChuongTrinh.Text;
                _dr["NoiDung"] = memoEdit_NoiDung.Text;
                _dr["GiaTri"] = txtGiaTri.Text;
                _dr["DoiTuong"] = txtDoiTuong.Text;
                _dr["NgayBatDau"] = Convert.ToDateTime(dateEdit_NgayBatDau.EditValue);
                _dr["NgayKetThuc"] = Convert.ToDateTime(dateEdit_NgayKetThuc.EditValue);
                //-------------------------
                string sql = @"select * from Promotion";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da);
                da.Update(ds, "TablePromotion");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool Validation(TextEdit[] list)
        {
            for (int i = 0; i < list.Length; i++)
            {
                if (String.IsNullOrEmpty(list[i].Text))
                {
                    MessageBox.Show("Có ít nhất một field dữ liệu bị rỗng");
                    list[i].Focus();
                    return false;
                }
            }
            if (String.IsNullOrEmpty(memoEdit_NoiDung.Text))
            {
                MessageBox.Show("Có ít nhất một field dữ liệu bị rỗng");
                memoEdit_NoiDung.Focus();
                return false;
            }
            else if(String.IsNullOrEmpty(dateEdit_NgayBatDau.Text) || String.IsNullOrEmpty(dateEdit_NgayKetThuc.Text))
            {
                MessageBox.Show("Có ít nhất một field dữ liệu bị rỗng");
                return false;
            }
            return true;
        }

        private void CheckButton()
        {
            try
            {
                if (_boqua)
                {
                    btnThem.Enabled = true;
                    btnLuu.Enabled = false;
                    btnSua.Enabled = false;
                    btnBoQua.Enabled = false;
                }
                else if (_insert)
                {
                    btnThem.Enabled = false;
                    btnLuu.Enabled = true;
                    btnSua.Enabled = false;
                    btnBoQua.Enabled = true;
                }
                else if (_edit)
                {
                    btnThem.Enabled = false;
                    btnLuu.Enabled = true;
                    btnSua.Enabled = false;
                    btnBoQua.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadToControl(DataRow dr)
        {
            try
            {
                if (dr != null)
                {
                    txtTenChuongTrinh.Text = dr["TenChuongTrinh"].ToString();
                    memoEdit_NoiDung.Text = dr["NoiDung"].ToString();
                    txtGiaTri.Text = dr["GiaTri"].ToString();
                    txtDoiTuong.Text = dr["DoiTuong"].ToString();
                    dateEdit_NgayBatDau.Text = dr["NgayBatDau"].ToString();
                    dateEdit_NgayKetThuc.Text = dr["NgayKetThuc"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ResetControl()
        {
            txtTenChuongTrinh.Text = "";
            memoEdit_NoiDung.Text = "";
            txtGiaTri.Text = "";
            txtDoiTuong.Text = "";
            dateEdit_NgayBatDau.Text = "";
            dateEdit_NgayKetThuc.Text = "";
        }
        #endregion
    }
}
